package com.neu.wms.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.AreaDto;
import com.neu.wms.entity.WmsArea;
import com.neu.wms.service.AreaService;
import com.neu.wms.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/location")
public class AreaController {
    @Autowired
    private AreaService areaService;
    @PostMapping("queryAll")
    public ResultVo selectAll(@RequestBody AreaDto areaDto)
    {
        PageHelper.startPage(areaDto.getCurrentPage(),areaDto.getPageSize(),true);
        List<WmsArea> area = areaService.selectAll(areaDto);
        PageInfo<WmsArea> pageInfo = new PageInfo<>(area);
        return ResultVo.ok().data("list",area).data("total",pageInfo.getTotal());
    }


    @GetMapping("{id}")
    public ResultVo queryById(@PathVariable("id") Long id){
        WmsArea wmsArea = areaService.selectByPrimaryKey(id);
        return ResultVo.ok().data("area",wmsArea);
    }

    @PostMapping("")
    public ResultVo add(@RequestBody  WmsArea area)
    {
        int i = areaService.insert(area);
        if (i>0) {
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }

    @PutMapping("")
    public ResultVo update(@RequestBody  WmsArea wmsArea)
    {
        int i = areaService.update(wmsArea);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @PutMapping("/quit")
    public ResultVo updateDelFlag(@RequestBody AreaDto areaDto)
    {
        if(areaDto.getDelFlag()==null)
            throw new IllegalArgumentException("Argument cannot be null or empty");
        int i = areaService.updateDelFlag(areaDto);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @DeleteMapping("{id}")
    public ResultVo delete(@PathVariable("id")Long id)
    {
        int i = areaService.delete(id);
        if (i>0) {
            return ResultVo.ok("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }
}
